//引入vue和vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		// 记录歌曲的播放状态
		playState: false,
		// 用来保存歌单的所有歌曲信息
		playList: null,
		// audio 对象
		innerAudioContext: null,
	},
	mutations: {
		upDataPlayState(state, val) {
			state.playState = val;
		},
		// 初始化播放器
		musicInit(state, url) {
			if (state.innerAudioContext) {
				state.innerAudioContext.destroy();
			}
			state.innerAudioContext = uni.createInnerAudioContext()
			state.innerAudioContext.autoplay = true;
			state.innerAudioContext.src = url;
			state.innerAudioContext.onPlay(() => {
				// console.log('开始播放');
			});
			state.innerAudioContext.onPause(() => {
				// console.log('暂停播放');
			});
			state.innerAudioContext.onStop(() => {
				// console.log('停止播放');
			});
			state.innerAudioContext.onEnded(() => {
				console.log('音频自然播放结束事件');
				this.$store.commit('upDataPlayState', false);
			});
			state.innerAudioContext.onTimeUpdate(() => {
				// console.log('音频播放进度更新事件');
			});
		},
		// 销毁播放器
		musicestroy(state) {
			if (state.innerAudioContext) {
				state.innerAudioContext.destroy();
			}
		}
	}
})
export default store
